package com.cn.wanxi.service;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务逻辑层
 * io
 * 数据来源于文件
 * 文件在哪个地方？
 * 文件名称是什么？
 * 有文件或者文件夹
 * 写入数据
 * 读取数据
 * 封装数据到对象
 */
public class CompanyService {


    public CompanyModel getCompanyModel() {

//        1.新建文件
//        IO.creatNote(PATH, FILENAME);
//        2.写入数据
        setDaoData();
//        3.读取数据
        String result = IO.readNote();
//        4.封装数据
//        setData();
//        CompanyDao companyDao = new CompanyDao();
//        companyModel.setPhone(companyDao.getPhone());
//        companyModel.setEmail(companyDao.getEmail());
//        companyModel.setLogo(companyDao.getLogo());
//        companyModel.setAddress(companyDao.getAddresss());
//        companyModel.setVersion(companyDao.getVersion());
//        companyModel.setNet(companyDao.getNet());
        return setData(result);
    }

    /**
     * 封装数据
     */
    private CompanyModel setData(String result) {
//        split通过分割字符将字符串转换为字符串数组
        String[] results = result.split(Path.SPLIT.getPath());
        CompanyModel companyModel = new CompanyModel();
        companyModel.setPhone(results[0]);
        companyModel.setEmail(results[3]);
        companyModel.setLogo(results[1]);
        companyModel.setAddress(results[4]);
        companyModel.setVersion(results[5]);
        companyModel.setNet(results[2]);
        return companyModel;
    }


    /**
     * 写入数据
     */
    private void setDaoData() {
        List<String> list = new ArrayList<>();
        CompanyDao companyDao = new CompanyDao();
        list.add(companyDao.getPhone());
        list.add(companyDao.getLogo());
        list.add(companyDao.getNet());
        list.add(companyDao.getEmail());
        list.add(companyDao.getAddresss());
        list.add(companyDao.getVersion());
        IO.writeNote(list, Path.FILE_COMPANY.getPath());
    }


}
